package elijah.base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

public class WaveProgressView extends View {
    public static final int MODE_CONSTRAINT = 0;
    public static final int MODE_FULL = 1;
    private int mode = MODE_CONSTRAINT;
    private int radius;
    private Paint paint;
    private RectF rectF;

    Path path;

    private float waveHeight = 0;

    private float rate = 0.75f;
    private ValueAnimator animator;

    public WaveProgressView(Context context) {
        this(context, null);
    }

    public WaveProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();


    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rectF = new RectF();
        path = new Path();


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.translate(radius, radius);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        canvas.drawCircle(0, 0, radius - 2, paint);

//

        float y = (radius - rate * radius * 2);
        float x = (float) Math.sqrt(radius * radius - y * y);

        float startAngle = (float) Math.toDegrees(Math.asin(y / radius));
        float sweepAngle = (90 - startAngle) * 2;
        paint.setStyle(Paint.Style.FILL);
        rectF.set(-radius, -radius, radius, radius);
        paint.setColor(0x33ff0000);
        drawWave(canvas, y, x, startAngle, sweepAngle, 1);
        paint.setColor(0xffff0000);
        drawWave(canvas, y, x, startAngle, sweepAngle, -1);


    }

    private void drawWave(Canvas canvas, float y, float x, float startAngle, float sweepAngle, int direction) {
        path.reset();
        path.moveTo(x, y);
        path.addArc(rectF, startAngle, sweepAngle);

        int count = 4;
        float step = 2 * x / count;
        float start = -x;

        for (int i = 0; i < count; i += 1) {
            path.quadTo(start + 1f / 2 * step, y - (waveHeight) * (x / 3f) * (i % 2 == 0 ? 1 : -1) * direction, start + step, y);
            start += step;
        }
        canvas.drawPath(path, paint);
    }

    LinearInterpolator interpolator = new LinearInterpolator();

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        radius = MeasureSpec.getSize(heightMeasureSpec) / 2;

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        animator = ValueAnimator.ofFloat(-1, 0, 1, 0, -1);
        animator.setInterpolator(interpolator);
        animator.setDuration(3000);
        animator.setRepeatCount(-1);
        animator.addUpdateListener(animation -> {
            waveHeight = (float) animation.getAnimatedValue();
            postInvalidate();
        });
        animator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        animator.cancel();
    }
}
